// service/enter/enter.js
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    safeBottom: `30px`,
    type: 'perfect',
    currentStep: 1,
    fileList: [],
    popupShow: false,
    cateShow: false,
    eduShow: false,
    eduArr: ['小学文化', '初中', '高中', '大专', '本科', '硕士', '博士'],
    experShow: false,
    experArr: ['1年以下', '1-3年', '3-5年', '5年以上'],
    nationShow: false,
    nationArr: [
      "汉族",
      "蒙古族",
      "回族",
      "藏族",
      "维吾尔族",
      "苗族",
      "彝族",
      "壮族",
      "布依族",
      "朝鲜族",
      "满族",
      "侗族",
      "瑶族",
      "白族",
      "土家族",
      "哈尼族",
      "哈萨克族",
      "傣族",
      "黎族",
      "傈僳族",
      "佤族",
      "畲族",
      "高山族",
      "拉祜族",
      "水族",
      "东乡族",
      "纳西族",
      "景颇族",
      "柯尔克孜族",
      "土族",
      "达斡尔族",
      "仫佬族",
      "羌族",
      "布朗族",
      "撒拉族",
      "毛难族",
      "仡佬族",
      "锡伯族",
      "阿昌族",
      "普米族",
      "塔吉克族",
      "怒族",
      "乌孜别克族",
      "俄罗斯族",
      "鄂温克族",
      "崩龙族",
      "保安族",
      "裕固族",
      "京族",
      "塔塔尔族",
      "独龙族",
      "鄂伦春族",
      "赫哲族",
      "门巴族",
      "珞巴族",
      "基诺族"
    ],
    name: '',
    sex: 0,
    idcard: '',
    front_image: '',
    opposite_image: '',
    user_image: '',
    certificate_image: '',
    health_image: '',
    cate: [],
    skill_cate_id: '',
    cateName: '',
    goodsNum: 0,
    goods_ids: '',
    image: '',
    images: [],
    edu: '',
    eduName: '',
    exper: '',
    experName: '',
    nation: '',
    nationName: '',
    province: '',
    city: '',
    district: '',
    address: '',
    lng: '',
    lat: '',
    note: '',
    state: '',
    face_open: 0,
    cateResult: [],
    cateResultValue: [],
    cateCheckAll: false
  },
  chooseAddress() {
    http.chooseLocation(this.data.lat,this.data.lng).then(res => {
      this.setData({
        province: res.province,
        city: res.city,
        district: res.district,
        address: res.name,
        lng: res.lng,
        lat: res.lat,
        goodsNum: 0,
        goods_ids: ''
      })
    })
  },
  setMan(e) {
    this.setData({
      sex: 0
    })
  },
  setWoman() {
    this.setData({
      sex: 1
    })
  },
  setAddress(e) {
    this.setData({
      address: e.detail.value
    })
  },
  setName(e) {
    this.setData({
      name: e.detail.value
    })
  },
  setIdcard(e) {
    this.setData({
      idcard: e.detail.value
    })
  },
  chooseFront() {
    http.chooseImg(['album', 'camera'], true).then(res => {
      this.setData({
        front_image: res.data.url
      })
    })
  },
  chooseOpposite() {
    http.chooseImg(['album', 'camera'], true).then(res => {
      this.setData({
        opposite_image: res.data.url
      })
    })
  },
  chooseUser() {
    http.chooseImg(['album', 'camera'], true).then(res => {
      this.setData({
        user_image: res.data.url
      })
    })
  },
  chooseCert() {
    http.chooseImg(['album', 'camera'], true).then(res => {
      this.setData({
        certificate_image: res.data.url
      })
    })
  },
  chooseHealth() {
    http.chooseImg(['album', 'camera'], true).then(res => {
      this.setData({
        health_image: res.data.url
      })
    })
  },
  chooseImage() {
    http.chooseImg(['album', 'camera'], true, true, '', ['original', 'compressed']).then(res => {
      this.setData({
        image: res.data.url
      })
    })
  },
  delImages(e){
    let index = e.currentTarget.dataset.index
    this.data.images.splice(index,1)
    this.setData({
      images: this.data.images
    })
  },
  chooseImages(e) {
    let index = e.currentTarget.dataset.index
    http.chooseImg(['album', 'camera'], true, true, this.data.token).then(res => {
      let arr = this.data.images;
      if(index !== undefined){
        arr.splice(index,1,res.data.url)
      }else {
        arr.push(res.data.url)
      }
      this.setData({
        images: arr
      })
    })
  },
  showCate() {
    this.setData({
      cateShow: true,
      cateResultValue: this.data.skill_cate_id ? this.data.skill_cate_id.split(',') : []
    })
  },
  showEdu() {
    this.setData({
      eduShow: true
    })
  },
  showExper() {
    this.setData({
      experShow: true
    })
  },
  showNation() {
    this.setData({
      nationShow: true
    })
  },
  closeCate() {
    this.setData({
      cateShow: false,
      cateResultValue: []
    })
  },
  closeEdu() {
    this.setData({
      eduShow: false
    })
  },
  closeExper() {
    this.setData({
      experShow: false
    })
  },
  closeNation() {
    this.setData({
      nationShow: false
    })
  },
  // confirm(e) {
  //   if (e.detail.value.id !== this.data.skill_cate_id) {
  //     this.setData({
  //       cateName: e.detail.value.name,
  //       skill_cate_id: e.detail.value.id,
  //       cateShow: false,
  //       goodsNum: 0,
  //       goods_ids: ''
  //     })
  //   }
  // },
  cateToggle(e) {
    console.log(e)
    const {item, index} = e.currentTarget.dataset;
    let {cateResult} = this.data;
    const checkbox = this.selectComponent(`.checkboxes-${item.id}`);
    console.log(checkbox)
    checkbox.toggle();
  },
  cateChange(e) {
    console.log(e)
    // const {item, index} = e.currentTarget.dataset;
    const {cate} = this.data;
    this.setData({
      cateResultValue: e.detail,
      cateCheckAll: (e.detail || []).length === cate.length
    });
  },
  confirmCate(e) {
    console.log(e)
    // if (e.detail.value.id !== this.data.skill_cate_id) {
      const {cateResultValue, cate} = this.data;
      let cateResultName = cate.filter(item => {
        return cateResultValue.includes(item.id + "")
      }).map(item => { return item.name }).join(',')
      console.log(cateResultName, cateResultValue)
      this.setData({
        // cateName: e.detail.value.name,
        // skill_cate_id: e.detail.value.id,
        cateName: cateResultName,
        skill_cate_id: cateResultValue.join(','),
        cateShow: false,
        goodsNum: 0,
        goods_ids: '',
        cateResultValue: []
      })
    // }
  },
  cateCheckAllToggle(){
    const checkbox = this.selectComponent(`.checkbox-all`);
    console.log(checkbox)
    checkbox.toggle();
  },
  cateCheckAllChange(e){
    const {cate} = this.data;
    let cateResultValue = []
    if(e.detail){
      cateResultValue = cate.map(item => { return item.id + "" })
    }
    this.setData({
      cateCheckAll: e.detail,
      cateResultValue: e.detail,
      cateResultValue
    });
  },
  confirmEdu(e) {
    this.setData({
      eduName: e.detail.value,
      edu: e.detail.index,
      eduShow: false,
    })
  },
  confirmExper(e) {
    this.setData({
      experName: e.detail.value,
      exper: e.detail.index,
      experShow: false,
    })
  },
  confirmNation(e) {
    this.setData({
      nation: e.detail.value,
      nationShow: false,
    })
  },

  chooseGoods() {
    if (this.data.province === '' || this.data.city === '') return util.toast('请先选择店位置信息')
    if (!this.data.skill_cate_id) return util.toast('请先选择职业技能')

    util.skip('/service/serveItem/serveItem?skill=' + this.data.skill_cate_id + '&goods_ids=' + this.data.goods_ids+'&city='+this.data.city, {
      selectGood: (data) => {
        this.setData({
          goodsNum: data.goods.length,
          goods_ids: data.goods.join(',')
        })
      }
    })
  },


  // 下一步
  nextStep() {
    if (this.data.currentStep === 1) {
      if (this.data.certificate_image.trim() === '') return util.toast('请上传从业资格证')
      if (this.data.health_image.trim() === '') return util.toast('请上传健康证')
      if (this.data.lat === '') return util.toast('请选择地址')
      if (this.data.address === '') return util.toast('请输入详细地址')
      if (this.data.skill_cate_id === '') return util.toast('请选择职业技能')
      if (this.data.goods_ids.trim() === '') return util.toast('请选择服务技能')
    }
    this.setData({
      currentStep: this.data.currentStep + 1
    })
  },
  // 上一步
  lastStep() {
    if (this.data.currentStep === 0) {
      util.back()
    } else {
      this.setData({
        currentStep: this.data.currentStep - 1
      })
    }

  },
  //返回首页
  toHome() {
    wx.switchTab({
      url: '/pages/index/index'
    })
  },
  apply() {
    if (this.data.image.trim() === '') return util.toast('请上传工装照')
    // if (this.data.images.length === 0) return util.toast('请上传生活照')
    if (this.data.edu === '') return util.toast('请选择学历')
    if (this.data.nation.trim() === '') return util.toast('请选择民族')
    if (this.data.exper === '') return util.toast('请选择经验')
    if (this.data.lat === '') return util.toast('请选择地址')
    if (this.data.address === '') return util.toast('请输入详细地址')
    const arr_jjlxr = this.data.arr_jjlxr;
    delete arr_jjlxr.__webviewId__;
    if(!arr_jjlxr) return util.toast('请填写紧急联系人')

    http.post('skill/apply', {
      type: this.data.type,
      name: this.data.name,
      sex: this.data.sex,
      idcard: this.data.idcard,
      front_image: this.data.front_image,
      opposite_image: this.data.opposite_image,
      user_image: this.data.user_image,
      certificate_image: this.data.certificate_image,
      health_image: this.data.health_image,
      skill_cate_id: this.data.skill_cate_id,
      goods_ids: this.data.goods_ids,
      image: this.data.image,
      images: this.data.images.join(','),
      edu: this.data.edu,
      exper: this.data.exper,
      nation: this.data.nation,
      province: this.data.province,
      city: this.data.city,
      district: this.data.district,
      address: this.data.address,
      lng: this.data.lng,
      lat: this.data.lat,
      ...this.data.arr_jjlxr
    }, true).then(res => {
      util.toast(res.msg)
      this.setData({
        state: 0
      })
    })
  },
  getInfo() {
    http.post('skillcate/getlist', '', true).then(res => {

      this.setData({
        cate: res.data
      })
    })
  },
  getResult() {
    http.post('skill/apply', {}, true, false).then(res => {
      if (res.data) {
        const arr_jjlxr = {
          urgent_name1: res.data.urgent_name1,
          urgent_mobile1:  res.data.urgent_mobile1,
          urgent_name2:  res.data.urgent_name2,
          urgent_mobile2:  res.data.urgent_mobile2,
          urgent_name3:  res.data.urgent_name3,
          urgent_mobile3:  res.data.urgent_mobile3,
        }
        this.setData({
          name: res.data.name,
          sex: res.data.sex,
          idcard: res.data.idcard,
          front_image: res.data.front_image,
          opposite_image: res.data.opposite_image,
          user_image: res.data.user_image,
          certificate_image: res.data.certificate_image,
          health_image: res.data.health_image,
          skill_cate_id: res.data.skill_cate_id,
          cateName: res.data.skillCateName,
          goods_ids: res.data.goods_ids,
          goodsNum: res.data.goods_ids.split(',').length,
          image: res.data.image,
          images: res.data.images.length>0 ? res.data.images.split(',') : [],
          edu: res.data.edu,
          eduName: this.data.eduArr[res.data.edu]  ? this.data.eduArr[res.data.edu] : '',
          exper: res.data.exper,
          experName: this.data.experArr[res.data.exper] ? this.data.experArr[res.data.exper] : '',
          nation: res.data.nation,
          province: res.data.province,
          city: res.data.city,
          district: res.data.district,
          address: res.data.address,
          lng: res.data.lng,
          lat: res.data.lat,
          note: res.data.note,
          state: Number(res.data.state),
          arr_jjlxr
        })
      }
    })
  },
  jjlxr(){
    wx.navigateTo({
      url: '/service/jjlxr/jjlxr',
    })
},
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const high = app.globalData.safeBottom
    this.setData({
      safeBottom: `${high}px`
    })
    this.getResult()
    this.getInfo()
  },

  onClose() {
    util.back()

  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    let arr_jjlxr=wx.getStorageSync('jjlxr')
    console.log(arr_jjlxr)
    this.setData({
      arr_jjlxr:arr_jjlxr
    })
    wx.removeStorageSync('jjlxr')
  },



  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})